package com.qf.controller;

import com.alipay.api.internal.util.AlipaySignature;
import com.qf.entity.TlPerson;
import com.qf.entity.TlTicket;
import com.qf.mapper.TicketMapper;
import com.qf.service.TicketService;
import com.qf.utils.AliPayTools2;
import com.qf.utils.PayMap;
import com.qf.utils.ResultMsg;
import com.qf.vo.TicketsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Autowired
    TicketService ticketService;
    @Autowired(required = false)
    TicketMapper ticketMapper;
    @Autowired
    AliPayTools2 aliPayTools2;


    //查询自己的订单订单
    @GetMapping("/findAll")
    public ResultMsg findAll(HttpServletRequest request){
        return ticketService.findAll(request);
    }


    //提交订单
    @PostMapping("/submit")
    public ResultMsg submit(@RequestBody Map<String,Integer> map){
        return ticketService.submit(map);
    }

    //取消订单
    @GetMapping("/delete")
    public ResultMsg delete(String ticketId){
        return ticketService.delete(ticketId);
    }

    @PostMapping("/topay")
    public ResultMsg topay(@RequestBody Map map){
        String ticketId = (String) map.get("ticketId");
        Integer ticketPrice = (Integer) map.get("ticketPrice");
        String url = aliPayTools2.saoma(ticketId, "车票订单", ticketPrice,"/ticket/notify");
        return ResultMsg.SUCCESS(url);
    }

    @PostMapping("/phonetopay")//手机端支付
    public ResultMsg phonetopay(@RequestBody Map map){
        String ticketId = (String) map.get("ticketId");
        Integer ticketPrice = (Integer) map.get("ticketPrice");
        String url = aliPayTools2.pay(ticketId, "车票订单", ticketPrice,"/ticket/notify");
        return ResultMsg.SUCCESS(url);
    }

    //回调
    @PostMapping("/notify/{gid}")
    public ResultMsg goodsnotify(@PathVariable("gid") String gid,HttpServletRequest request)throws Exception{
//二次验签：request的值转为map
        Map<String, String> paramsMap = PayMap.converToMap(request);
//将异步通知中收到的所有参数都存放到 map 中
        boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, AliPayTools2.alipayPublicPay, "utf-8", "RSA2"); //调用SDK验证签名
        if (signVerified){
// TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商家自身业务处理，校验失败返回failure

            String status = paramsMap.get("trade_status");
            if(status.equals("TRADE_SUCCESS")){
//订单状态的修改
                TlTicket tlTicket = ticketMapper.selectById(gid);
                tlTicket.setTicketFlag(1);
                ticketMapper.updateById(tlTicket);
            }
            return ResultMsg.SUCCESS();
        } /*else {
// TODO 验签失败则记录异常日志，并在response中返回failure.
}
return null;*/
        throw new RuntimeException("支付验签失败");
    }

    //提交订单
    @PostMapping("/send")
    public ResultMsg send(@RequestBody TicketsVo ticketsVo,HttpServletRequest request){
        List<TlPerson> personList = ticketsVo.getPersonList();
        return ticketService.create(ticketsVo,request);
    }

    @PostMapping("/refund")
    public ResultMsg refund(@RequestBody Map map){
        String ticketId = (String) map.get("ticketId");
        Integer ticketPrice = (Integer) map.get("ticketPrice");
        return ticketService.refund(ticketId,ticketPrice);
    }

}
